;(function (w,$,doc) {
    var config = {
        'table': 'table',
        'thead': '#thead',
        'tbody': 'tbody',
        'fixClass': 'fixed'
    };
    var fixedRow = function(options) {
        this.option =  options;
        this.table = $(this.option.table);
        this.oTbody = this.table.find(this.option.tbody);
        this.oHead = this.table.find(this.option.thead);
        this.oHeadWidth = this.oHead.outerWidth();
        this.oHeadHeight= this.oHead.outerHeight()
        var isInView = this.isInView();
        this.addEvt();
    };
    fixedRow.prototype = {
        isInView: function() {
            // 判断是否在视口内 ture则为是
           var $w = $(w),
                winScrollTop = $w.scrollTop(),
                winH = $w.height(),
                tabOffTop = this.table.offset().top,
                tabOutH = this.table.outerHeight();
            return (winScrollTop <= tabOffTop + tabOutH) && (winScrollTop + winH >= tabOffTop);
        },
        addEvt: function() {
            var that = this;
            $(doc).on('scroll.fixedTable',function() {
                var head = that.oHead,
                    table = that.table,
                    tbody = that.oTbody,
                    nWidth = that.oHeadWidth,
                    nHeight = that.oHeadHeight,
                    res = head.offset().top - document.body.scrollTop;
                console.log(nWidth)
                // 如果表格在可视范围内
                if(that.isInView()) {
                    // 表格即将离开可视区域
                    if(res <= 0) {
                        var isFixed = table.find('.replace-head').length;
                        // 如果占位表头不存在，增加一个表头占原来的位置
                        if(!isFixed) {
                            var aHeadTd = head.find('td');
                            var oCopy = head.clone();
                            oCopy.prop('id','')
                                .addClass('replace-head')
                                .css('visibility','hidden')
                                .outerWidth(nWidth)
                                .outerHeight(nHeight);
                           table.prepend(oCopy);
                        }
                        // 如果存在
                        else {
                            $('.replace-head').show();
                        }
                        head.addClass('fixed')
                            .width(nWidth)
                            .find('tr').width(nWidth);
                    }
                    // 是否为滚动条向上，表头垂直往下，与视口的距离小于0
                    var oBodyTop = tbody.offset().top - document.body.scrollTop;
                    if(oBodyTop >= head.outerHeight(true)) {
                        head.removeClass('fixed').width(nWidth);
                        $('.replace-head').hide();
                    }
                }
                // 表格离开可视区域
                else {
                    $('.replace-head').hide();
                    head.removeClass('fixed').width(nWidth);
                }
            })
        }
    };

    $.fn.fixedRow = function(options) {
        this.each(function() {
            var options = $.extend({},config,options);
            return new fixedRow(options);
        })
    };
}(window,jQuery,document));
